<?php


session_start();
require_once('../config.php');

require_once($CONFIG->datadir.'/lib/db/DB.php');


//////////////////////////////////////////////////


/**
 *  Based on http://www.desarrolloweb.com/articulos/codigo-php-nube-etiquetas.html
 */
function keywordscloud($keywords) {

   global $CONFIG;

   $meta = $_SESSION["meta"];

   $valor_max = max($keywords);
   $valor_min = min($keywords);
   $diferencia = $valor_max - $valor_min;
   
   $cloudstylepath = $CONFIG->webdir.'/styles/'.$CONFIG->style_name.'/cloud.css';
   echo '<link rel="stylesheet" type="text/css" href="'.$cloudstylepath.'" />';

   echo '<div class="clouddiv">';
   echo '<div class="keywordsdiv">';
   
   foreach ($keywords as $keywordname => $apariciones) {

      // % from 0 to 10 to assign a style
      $valor_relativo = round((($apariciones - $valor_min) / $diferencia) * $CONFIG->style_cloudmin);

      echo '<span class="keywordsize_'.$valor_relativo.'">';
      echo '<a href="'.$CONFIG->webdir.'/search/?q='.$keywordname.'&send=1&meta='.$meta.'">'.$keywordname.'</a>';
      echo "</span> ";
   }

   echo "<div style='clear:both'></div>";
   echo '</div>';
   echo '</div>';
} 


/////////////////////////////////////////////

// DB instance
$DB = new DB();


// Get most related keywords
$sql = "SELECT keywordid, count(keywordid) as count 
        FROM entrykeywords group by keywordid
        ORDER BY count DESC 
        LIMIT 0, 20";
$keywords = $DB->selectResponses($sql);

$keywordsarray = array();
foreach ($keywords as $keyword) {

    if ($keyword->count > $CONFIG->couldmin) {

        $keywordnamesql = "SELECT word FROM keywords WHERE id = ?";
        $keywordname = $DB->selectResponse($keywordnamesql, array($keyword->keywordid));

        if ($keywordname) {
            $keywordsarray[html_entity_decode($keywordname->word)] = $keyword->count;
        }
    }

}

if (!empty($keywordsarray)) {

    keywordscloud($keywordsarray);

} else {
    echo $string['nokeywords'];
}

?>
